package com.suneast.suneaststore.modules.homepage.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.homepage.ServiceIntroBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zestz on 2018/4/4.
 */

public class ServiceIntroAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int RIGHT = 0;
    private static final int LEFT = 1;

    private ArrayList<ServiceIntroBean> dataList;


    public ServiceIntroAdapter(ArrayList<ServiceIntroBean> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case RIGHT: {
                view = LayoutInflater
                        .from(parent.getContext())
                        .inflate(R.layout.item_service_intro_1, parent, false);
                return new LeftViewHolder(view);
            }
            case LEFT: {
                view = LayoutInflater
                        .from(parent.getContext())
                        .inflate(R.layout.item_service_intro_2, parent, false);
                return new LeftViewHolder(view);
            }
        }

        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof RightViewHolder) {
            RightViewHolder rHolder = (RightViewHolder) holder;
            rHolder.serviceNameTv.setText(dataList.get(position).getName());
            rHolder.serviceIntroTv.setText(dataList.get(position).getIntro());
            Glide.with(holder.itemView.getContext()).load(dataList.get(position).getImgUrl())
                    .into(rHolder.serviceIntroImg);

        } else if (holder instanceof LeftViewHolder) {
            LeftViewHolder lHolder = (LeftViewHolder) holder;
            lHolder.serviceNameTv.setText(dataList.get(position).getName());
            lHolder.serviceIntroTv.setText(dataList.get(position).getIntro());
            Glide.with(holder.itemView.getContext()).load(dataList.get(position).getImgUrl())
                    .into(lHolder.serviceIntroImg);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(dataList.get(position).getId());
                }
            }
        });
        switch (position) {
            case 0: {
                holder.itemView.setBackground(holder.itemView.getResources().getDrawable(R.drawable.corner_service_type_1));
            }
            break;
            case 1: {
                holder.itemView.setBackground(holder.itemView.getResources().getDrawable(R.drawable.corner_service_type_2));
            }
            break;
            case 2: {
                holder.itemView.setBackground(holder.itemView.getResources().getDrawable(R.drawable.corner_service_type_3));
            }
            break;
            case 3: {
                holder.itemView.setBackground(holder.itemView.getResources().getDrawable(R.drawable.corner_service_type_4));
            }
            break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        return position % 2;
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    static class RightViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_service_name)
        TextView serviceNameTv;

        @BindView(R.id.tv_service_intro)
        TextView serviceIntroTv;

        @BindView(R.id.circle_img_service_intro)
        ImageView serviceIntroImg;

        public RightViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class LeftViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_service_name)
        TextView serviceNameTv;

        @BindView(R.id.tv_service_intro)
        TextView serviceIntroTv;

        @BindView(R.id.circle_img_service_intro)
        ImageView serviceIntroImg;

        public LeftViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    private OnClickListener listener;

    public interface OnClickListener {
        void onClick(int id);
    }
}
